package com.ruoyi.doctorlibrary.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.disease.domain.Disease;
import com.ruoyi.disease.mapper.DiseaseMapper;
import com.ruoyi.doctordisease.domain.DoctorDisease;
import com.ruoyi.doctordisease.domain.vo.DoctorDiseaseVO;
import com.ruoyi.doctordisease.mapper.DoctorDiseaseMapper;
import com.ruoyi.doctorlibrary.domain.vo.DoctorLibraryVO;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.doctorlibrary.mapper.DoctorLibraryMapper;
import com.ruoyi.doctorlibrary.domain.DoctorLibrary;
import com.ruoyi.doctorlibrary.service.IDoctorLibraryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【医生库】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-02
 */
@Service
public class DoctorLibraryServiceImpl implements IDoctorLibraryService 
{
    @Autowired
    private DoctorLibraryMapper doctorLibraryMapper;

    @Autowired
    private DoctorDiseaseMapper doctorDiseaseMapper;
    @Autowired
    private DiseaseMapper diseaseMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param doctorId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DoctorLibrary selectDoctorLibraryByDoctorId(Long doctorId)
    {
        return doctorLibraryMapper.selectDoctorLibraryByDoctorId(doctorId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param doctorDiseaseVO 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DoctorDiseaseVO> selectDoctorLibraryList(DoctorDiseaseVO doctorDiseaseVO)
    {
        DoctorLibrary doctorLibrary = new DoctorLibrary();
        BeanUtils.copyProperties(doctorDiseaseVO, doctorLibrary);
        List<DoctorLibrary> doctorLibraries = doctorLibraryMapper.selectDoctorLibraryList(doctorLibrary);
        List<DoctorDiseaseVO> doctorDiseaseVOS = new ArrayList<DoctorDiseaseVO>();
        for (DoctorLibrary library : doctorLibraries) {
            DoctorDiseaseVO doctorDiseaseVO1 = new DoctorDiseaseVO();
            BeanUtils.copyProperties(library, doctorDiseaseVO1);
            DoctorDisease doctorDisease = new DoctorDisease();
            doctorDisease.setDoctorId(library.getDoctorId());
            ArrayList<Long> longs = new ArrayList<Long>();
            List<DoctorDisease> doctorDiseases = doctorDiseaseMapper.selectDoctorDiseaseList(doctorDisease);
            for (DoctorDisease disease : doctorDiseases) {
                longs.add(disease.getDiseaseId());
            }
            User user = userMapper.selectUserByUserId(library.getUserId());
            if (user.getName() != null && !user.getName().isEmpty()) {
                doctorDiseaseVO1.setName(user.getName());
            }
            doctorDiseaseVO1.setDiseaseIds(longs);
            doctorDiseaseVOS.add(doctorDiseaseVO1);
        }
        return doctorDiseaseVOS;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param doctorLibrary 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDoctorLibrary(DoctorLibraryVO doctorLibrary)
    {
        DoctorLibrary doctorLibrary1 = new DoctorLibrary();
        BeanUtils.copyProperties(doctorLibrary, doctorLibrary1);
        doctorLibrary1.setCreateTime(DateUtils.getNowDate());
        doctorLibraryMapper.insertDoctorLibrary(doctorLibrary1);
        DoctorDisease doctorDisease = new DoctorDisease();
        doctorDisease.setDoctorId(doctorLibrary1.getDoctorId());
        doctorDisease.setDiseaseId(doctorLibrary.getDisease().getDiseaseId());
        doctorDiseaseMapper.insertDoctorDisease(doctorDisease);
        return 1;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param doctorLibrary 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDoctorLibrary(DoctorLibrary doctorLibrary)
    {
        return doctorLibraryMapper.updateDoctorLibrary(doctorLibrary);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param doctorIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDoctorLibraryByDoctorIds(Long[] doctorIds)
    {
        return doctorLibraryMapper.deleteDoctorLibraryByDoctorIds(doctorIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param doctorId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDoctorLibraryByDoctorId(Long doctorId)
    {
        return doctorLibraryMapper.deleteDoctorLibraryByDoctorId(doctorId);
    }

    /**
     * 查询医生库
     *
     * @param doctorLibrary
     * @return
     */
    @Override
    public List<DoctorLibraryVO> selectDoctorLibrarys(DoctorLibrary doctorLibrary) {
        List<DoctorLibrary> doctorLibraries = doctorLibraryMapper.selectDoctorLibraryList(doctorLibrary);
        List<DoctorLibraryVO> doctorLibraryVOList = new ArrayList<>();
        for (DoctorLibrary library : doctorLibraries) {
            DoctorLibraryVO doctorDiseaseVO1 = new DoctorLibraryVO();
            BeanUtils.copyProperties(library, doctorDiseaseVO1);
            DoctorDisease doctorDisease = doctorDiseaseMapper.selectDoctorDiseaseByDoctorId(library.getDoctorId());
            Disease disease = diseaseMapper.selectDiseaseByDiseaseId(doctorDisease.getDiseaseId());
            doctorDiseaseVO1.setDisease(disease);
            doctorLibraryVOList.add(doctorDiseaseVO1);
        }
        return doctorLibraryVOList;
    }

    /**
     * 修改医生
     *
     * @param doctorLibrary
     * @return
     */
    @Override
    @Transactional
    public String updateDoctor(DoctorLibraryVO doctorLibrary) {
        DoctorLibrary doctorLibrary1 = new DoctorLibrary();
        BeanUtils.copyProperties(doctorLibrary, doctorLibrary1);
        doctorLibraryMapper.updateDoctorLibrary(doctorLibrary1);
        DoctorDisease doctorDisease = new DoctorDisease();
        doctorDisease.setDoctorId(doctorLibrary1.getDoctorId());
        doctorDisease.setDiseaseId(doctorLibrary.getDisease().getDiseaseId());
        doctorDiseaseMapper.updateDoctorDisease(doctorDisease);
        return "修改成功";
    }
}
